﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace HostTool.Util
{
  public  class IniDocument
    {

        Dictionary<string, Dictionary<string, string>> dicSections = new Dictionary<string, Dictionary<string, string>>();

        public IniDocument(Stream stream)
        {

            using (StreamReader sr = new StreamReader(stream, Encoding.Default))
            {

                string sectionName = "";
                while (true)
                {
                    string lineTxt = sr.ReadLine();
                    if (lineTxt == null) break;
                    lineTxt = lineTxt.Trim();
                    if (lineTxt.Length == 0) continue;

                    if (lineTxt.StartsWith(";")) continue;
                    if (lineTxt.StartsWith("//")) continue;

                    if (lineTxt.StartsWith("["))
                    {
                        int index = lineTxt.IndexOf(']');
                        if (index > 1)
                        {
                            sectionName = lineTxt.Substring(1, index - 1);
                            if (dicSections.ContainsKey(sectionName) == false)
                            {
                                dicSections.Add(sectionName, new Dictionary<string, string>());
                            }
                        }
                    }
                    else if (sectionName != "")
                    {
                        int index = lineTxt.IndexOf('=');
                        if (index >= 1 && (index < lineTxt.Length - 1))
                        {
                            string key = lineTxt.Substring(0, index).Trim();
                            if (key != "" && dicSections[sectionName].ContainsKey(key) == false)
                            {
                                string value = lineTxt.Substring(index + 1, lineTxt.Length - index - 1);
                                index = value.IndexOf("//");
                                if (index < 0)
                                {
                                    index = value.IndexOf(";");
                                }
                                if (index >= 0)
                                {
                                    value = value.Substring(0, index).Trim();
                                }
                                dicSections[sectionName].Add(key, value);

                            }


                        }
                    }



                }


            }

        }


        public int GetInt(string section, string key, int defVal)
        {
            string valStr = GetString(section, key);
            int val = defVal;
            if (valStr != "")
            {
                string valLow = valStr.ToLower();
               
                if (valLow.StartsWith("0x")) //16进制
                {
                   
                    int.TryParse(valLow.Substring(2), System.Globalization.NumberStyles.AllowHexSpecifier, null, out val);
                }
                else
                {
                   int.TryParse(valLow, out val);
                }
            }
            return val;
        }

        public string GetString(string section, string key)
        {
            if (dicSections.ContainsKey(section))
            {
                if (dicSections[section].ContainsKey(key))
                {
                    return dicSections[section][key];
                }
            }

            return "";
        }

        public int[] GetIntList(string section, string key)
        {
            string valList = GetString(section, key);
            if (valList == "") return null;

            List<int> list = new List<int>();
            string[] array = null;
            if (valList.Contains("，")) //全拼的，
            {
                array = valList.Split(new char[] { '，'},StringSplitOptions.RemoveEmptyEntries);
            }
            else if (valList.Contains(",")) //半角
            {
                array = valList.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            }
            if (array != null && array.Length > 0)
            {
                for (int i = 0; i < array.Length; i++)
                {
                    string lowStr=array[i].ToLower();
                    int val = 0;
                    if (lowStr.StartsWith("0x"))
                    {
                        int.TryParse(lowStr.Substring(2), System.Globalization.NumberStyles.AllowHexSpecifier, null, out val);
                    }
                    else
                    {
                        int.TryParse(lowStr, out val);
                    }
                    list.Add(val);
                }
            }
            return list.ToArray();
        }

    }
}
